package com.example.demo.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.*;
import com.example.demo.entity.excel.CheckExportEntity;
import com.example.demo.mapper.ScAttendanceTypeMapper;
import com.example.demo.mapper.ScShiftMapper;
import com.example.demo.mapper.ScSourceMapper;
import com.example.demo.service.ScRecordService;
import com.example.demo.mapper.ScRecordMapper;
import com.example.demo.utils.DateTransferUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ScRecordServiceImpl extends ServiceImpl<ScRecordMapper, ScRecord>
    implements ScRecordService{
    private static SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM");


    @Autowired
    ScRecordMapper scRecordMapper;
    @Autowired
    ScAttendanceTypeMapper scAttendanceTypeMapper;
    @Autowired
    ScSourceMapper scSourceMapper;
    @Autowired
    ScSourceServiceImpl scSourceService;
    @Autowired
    ScShiftMapper scShiftMapper;

    @Override
    public Map<String, Object> export1(String date1, String date2, String department) {
        Map<String, Object> excelMap = new HashMap<>();
        excelMap.put("date1", date1);
        excelMap.put("date2", date2);

        if (StrUtil.isEmpty(department)) department = "%";
        List<Map<Object, Object>> map = scRecordMapper.getSum(date1, date2, department);
        List<CheckExportEntity> list = new ArrayList<>(); //存结果对象的list

        int i = 1;
        int right = 0;
        int wrong = 0;
        for (Map<Object, Object> item : map){
            CheckExportEntity entity = new CheckExportEntity();
            //获取这个人所属的考勤时段
            List<ScRecord> scRecords = scRecordMapper.getByName((String) item.get("name"));
            ScRecord scRecord = scRecords.get(0);
            ScAttendanceType scAttendanceType = scAttendanceTypeMapper.getByName(scRecord.getAttendanceType());

            entity.setIndex(i);
            entity.setName(scRecord.getName());
            entity.setType(scRecord.getAttendanceType());
            entity.setShouldDay(scAttendanceType.getDays()); //应出勤日期
            entity.setDay((BigDecimal) item.get("days"));
            entity.setLackDay((BigDecimal) item.get("lack"));
            entity.setShould(scAttendanceType.getMonthWork1());
            entity.setAmount((BigDecimal) item.get("work_time"));
            entity.setSub(entity.getShould().subtract(entity.getAmount()));
            if (entity.getSub().compareTo(new BigDecimal(0)) > 0) wrong +=1;
            else right +=1;
            list.add(entity);
            i++;
        }
        excelMap.put("list", list);
        excelMap.put("right", right);
        excelMap.put("wrong", wrong);
        return excelMap;
    }

    /**
     * 循环截取某页列表进行分页
     */
    @Override
    public List<CheckExportEntity> page(List<CheckExportEntity> dataList, int pageSize, int currentPage) {
        List<CheckExportEntity> currentPageList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
                CheckExportEntity data = dataList.get(currIdx + i);
                currentPageList.add(data);
            }
        }
        return currentPageList;
    }

    @Override
    public JSONArray getRank(String date1, String date2, String department) {
        List<HashMap<String, BigDecimal>> list = scRecordMapper.getRank(date1, date2, department);
        JSONArray array = new JSONArray();
        List<String> list1 = new ArrayList<>(); //存储姓名
        List<BigDecimal> list2 = new ArrayList<>(); //存储工作时长
        if (list.size() == 0){
            return null;
        }
        int i = 0;
        while (i < list.size()){
            list1.add(String.valueOf(list.get(i).get("name")));
            list2.add(list.get(i).get("work_time"));
            i++;
        }
        array.add(list1);
        array.add(list2);
        return array;
    }

    public BigDecimal calculate1(String date1, String date2) throws ParseException {
        Date begin1 = dateFormat1.parse(date1); //实际上班时间
        Date end1 = dateFormat1.parse(date2); //实际下班时间
        BigDecimal bd;
        long period = DateUtil.between(begin1, end1, DateUnit.MINUTE);

        bd = new BigDecimal(period / 60.0);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd;
    }

    public List<String> getDate(String date){
        List<String> list = new ArrayList<>();
        try {
            // 将日期时间字符串转化为Date对象
            Date dateTime = dateFormat2.parse(date);
            // 验证日期时间字符串的格式和有效性成功
            list.add(DateTransferUtil.getFirstDay(date));
            list.add(DateTransferUtil.getLastDay(date));
        } catch (ParseException e) {
            return null;
        }
        return list;
    }
}




